<template>
  <transition name='slide' appear>
  <music-list :rank='true' :title='title' :bg-image='bgImage' :songs='songs'></music-list>
  </transition>
</template>

<script>
  import MusicList from 'components/music-list/music-list'
  import { mapGetters } from 'vuex'
  import { getDetail } from 'api'
  import { createSong } from 'common/js/song'

  export default {
    name: '',
    props: {},
    data() {
      return {
        songs: []
      }
    },
    created() {
      this._getDetail()
    },
    mounted() {
    },
    destroyed() {
    },
    methods: {
      _getDetail() {
        if (!this.toplist.id) {
          this.$router.push('/rank')
          return
        }
        let params = { id: this.toplist.id }
        getDetail(params).then(res => {
          this.songs = this._normalizeSong(res.data.playlist.tracks)
          console.log(this.songs)
        })
      },
      _normalizeSong(list) {
        let ret = []
        list.forEach((item) => {
          if (item.id) {
            let song = createSong(item)
            ret.push(song)
          }
        })
        return ret
      }
    },
    computed: {
      title() {
        return this.toplist.name
      },
      bgImage() {
        if (this.songs.length) {
          return this.songs[0].image
        }
        return ''
      },
      ...mapGetters([
        'toplist'
      ])
    },
    watch: {},
    components: {
      MusicList
    }
  }
</script>

<style lang="stylus" scoped>
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s
  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
